<?php

/* This notice must be untouched at all times.

editdata.php    v. 2.2
This file is part of PhyloGeoViz
The latest version is available at
http://www.phylogeoviz.com

Copyright (c) 2007 Yi-Hsin Erica Tsai. All rights reserved.
Created 15.7.2007 by Yi-Hsin Erica Tsai (Web: http://www.duke.edu/~yet2)
Last modified: 20.8.2007

PhyloGeoViz is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

PhyloGeoViz is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function pv ($var)
{
	echo "<pre>";
	var_dump($var);
	echo "</pre>\n";
}

$numpops = $_REQUEST["numpops"];
$numhaps = $_REQUEST["full_numhaps"];
$pop_names = json_decode(stripslashes($_REQUEST["pop_names"])); //json_decode is the php function that parses encoded JSON objects (evalJSON() was the javascript function)
$hap_names = json_decode(stripslashes($_REQUEST["full_hap_names"])); 
$pop_lat = json_decode(stripslashes($_REQUEST["pop_lat"])); 
$pop_lng = json_decode(stripslashes($_REQUEST["pop_lng"])); 
$pop_haps = json_decode(stripslashes($_REQUEST["full_pop_haps"])); 
$hapgroups = json_decode(stripslashes($_REQUEST["full_hapgroups"])); 
$pop_include = json_decode(stripslashes($_REQUEST["pop_include"])); //json_decode 
$hap_include = json_decode(stripslashes($_REQUEST["hap_include"])); //json_decode 
$uniquegroups = json_decode(stripslashes($_REQUEST["uniquegroups"])); 
$radii_in_km = json_decode(stripslashes($_REQUEST["radii_in_km"])); 
$set_hap_colors = json_decode(stripslashes($_REQUEST["set_hap_colors"])); 
//$set_hap_colors = $_REQUEST["set_hap_colors"]; 
//pv($uniquegroups);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Data management</title>
    <link type="text/css" rel="stylesheet" href="phylogeoviz.css" />
    <link type="text/css" rel="stylesheet" href="previewstyle.css" />
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAxhj9keQ-W_pSFQOerM1zcxQDzKVN9nYxJYcr0zvm4oc_VslaGBS9ekLzJd0hyV9ctr3fGo93wfu7EA"
	        type="text/javascript"></script>
    <script type="text/javascript" src="javascripts/prototype.js"></script>
	<script type="text/javascript" src="javascripts/scriptaculous.js"></script>
	<script type="text/javascript" src="phylogeovizmapfunctions.js"></script>
    <script type="text/javascript" src="javascripts/jquery-1.1.3.1.js"></script>
 	<script type="text/javascript">
    //<![CDATA[
    
    jQuery.noConflict();  //tells jQuery not to use '$'; prototype does instead
    
	var numpops;
	var numhaps;
	var pop_lat;
	var pop_lng;
	var pop_haps;
	var pop_names;  //array of strings that store the names of each population.  The length of pop_names = numpops.
	var hapgroups;
	var hap_names;
	var pop_include;
	var hap_include;
	var pop_radii;
	var radii_in_km;
	var uniquegroups;

	//Event handlers
	//The handler functions are called onchange of the corresponding data fields.
	function pop_names_handler(element)
	{
		var index = Number(element.id.substr(5));  //index is value after 'pname' string
		pop_names[index] = element.value;
	}
	
	function pop_haps_handler(element)
	{
		var pindex = Number(element.id.substring(1, element.id.indexOf('h')));
		var hindex = Number(element.id.substr(element.id.indexOf('h') + 1));
		
		pop_haps[pindex][hindex] = Number(element.value);
	}
	
	function pop_lat_handler(element)
	{
		var pindex = Number(element.id.substr(4));
		pop_lat[pindex] = Number(element.value);
	}
	
	function pop_lng_handler(element)
	{
		var pindex = Number(element.id.substr(4));
		pop_lng[pindex] = Number(element.value);
	}
	
	function hapgroups_handler(element)
	{
		var hindex = Number(element.id.substr(6));
		if (element.value == "Add a new group")
		{
			var possible = window.prompt("Enter new group name here:", "");
			if(possible)  //if user enters new value
			{
				//add new group to all haplotype menus
				for (var h = 0; h < numhaps; ++h)
				{
					var newgroup = document.createElement('option');
					newgroup.value = possible;
					newgroup.innerHTML = possible;
					var alloptions = jQuery("option", $("hgroup" + h));
					var addnewnode = $("hgroup" + h).removeChild(alloptions[alloptions.length - 1]);
					
					$("hgroup" + h).appendChild(newgroup);
					$("hgroup" + h).appendChild(addnewnode);
				}

				hapgroups[hindex] = possible;
				element.value = possible;
			}
			
			else //if user clicks cancel, returns null
				return;  //user cancelled, so do nothing
		}
		
		else
			hapgroups[hindex] = element.value;
	}
	
	function hapname_handler(element)
	{
		var hindex = Number(element.id.substr(6));
		hap_names[hindex] = element.value;
	}

	
	function pis_include_handler(element)
	{
		if (element.checked == false)
			$("np").innerHTML = Number($("np").innerHTML) - 1;
		else
			$("np").innerHTML = Number($("np").innerHTML) + 1;
	}

	function his_include_handler(element)
	{
		if (element.checked == false)
			$("nh").innerHTML = Number($("nh").innerHTML) - 1;
		else
			$("nh").innerHTML = Number($("nh").innerHTML) + 1;
	}

	function load()
	{
<?php
		///////////////  Setting the input values ///////////////////
		
		echo ("\t\tnumpops = ".$numpops.";\n");
		echo ("\t\tnumhaps = ".$numhaps.";\n");
		echo ("\t\tpop_names = new Array();\n");
		echo ("\t\tpop_lat = new Array();\n");
		echo ("\t\tpop_lng = new Array();\n");
		echo ("\t\tpop_haps = new Array();\n");
		echo ("\t\thapgroups = new Array();\n");
		echo ("\t\thap_names = new Array();\n");
		echo ("\t\tpop_include = new Array();\n");
		echo ("\t\thap_include = new Array();\n");
		echo ("\t\tradii_in_km = new Array();\n");
		echo ("\t\tuniquegroups = new Array();\n");
		echo ("\t\tset_hap_colors = new Array();\n");
		for ($i = 0; $i < $numpops; $i++)
		{
			//if I had this to do over again, I would make the array index for pop_centers and pop_haps be the pop_name and then use foreach loops throughout the rest of the code.  But that would be too much work now, ah well...
			echo ("\t\tpop_names[".$i."] = \"{$pop_names[$i]}\";\n");
			echo ("\t\tpop_lat[".$i."] = {$pop_lat[$i]};\n");
			echo ("\t\tpop_lng[".$i."] = {$pop_lng[$i]};\n");
			echo ("\t\tpop_include[".$i."] = {$pop_include[$i]};\n");
			echo ("\t\tpop_haps[".$i."] = new Array();\n");
			
			for ($j = 0; $j < $numhaps; $j++)
			{
				echo ("\t\tpop_haps[".$i."][".$j."] = {$pop_haps[$i][$j]};\n");
			}
		}
		
		for ($h = 0; $h < $numhaps; $h++)
		{
			echo ("\t\thap_names[".$h."] = \"{$hap_names[$h]}\";\n");
			echo ("\t\thapgroups[".$h."] = \"{$hapgroups[$h]}\";\n");
			echo ("\t\thap_include[".$h."] = \"{$hap_include[$h]}\";\n");
		}

		for ($g = 0; $g < count($uniquegroups); $g++)
		{
			echo ("\t\tuniquegroups[".$g."] = \"{$uniquegroups[$g]}\";\n");
		}
		
		for ($i = 0; $i < count($radii_in_km); $i++)
		{
			echo ("\t\tradii_in_km[".$i."] = \"{$radii_in_km[$i]}\";\n");
		}
		
		for ($i = 0; $i < count($set_hap_colors); $i++)
		{
			echo ("\t\tset_hap_colors[".$i."] = new Array();\n");
			echo ("\t\tset_hap_colors[".$i."][0] = \"".$set_hap_colors[$i][0]."\";\n");
			echo ("\t\tset_hap_colors[".$i."][1] = \"".$set_hap_colors[$i][1]."\";\n");
		}
?>
		////////////// print out the read in values in the table ////////
		
		for (var p = 0; p < numpops; ++p)
		{
			$("pname"+p).value = pop_names[p];
			$("plat"+p).value = pop_lat[p];
			$("plng"+p).value = pop_lng[p];
			
			if (pop_include[p] > 0)
				$("p"+p+"is_include").checked = true;
			else
				$("p"+p+"is_include").checked = false;
				
			for (var h = 0; h < numhaps; ++h)
				$("p"+p+"h"+h).value = pop_haps[p][h];
		}

		for (var h = 0; h < numhaps; ++h)
		{
			$("hname"+h).value = hap_names[h];
			$("hgroup"+h).value = hapgroups[h];
			if (hap_include[h] > 0)
				$("h"+h+"is_include").checked = true;
			else
				$("h"+h+"is_include").checked = false;
		}

		$("np").innerHTML = 0;
		$("nh").innerHTML = 0;

		//set display values for numpops and numhaps
		for (p = 0; p < numpops; ++p)
		{
			if ($("p"+p+"is_include").checked == true)
				$("np").innerHTML = Number($("np").innerHTML) + 1;
		}

		for (h = 0; h < numhaps; ++h)
		{
			if ($("h"+h+"is_include").checked == true)
				$("nh").innerHTML = Number($("nh").innerHTML) + 1;
		}
	}
	
	//]]>
</script>
</head>
	
<body onload="load()">
  <div id="page">
  <h2>Manage your data</h2>
  <p>To edit a particular data entry, just click in the cell you want to change and start typing or select the option you want from the drop down menu.  To be certain that all changes have been recorded, click in a different cell when finished.  When finished editing your data, click 'Update Map'.</p>
  <div id="datadiv">
	<form id="datamatrix" name="datamatrix">
	<table id="datatable" name="datatable">
	    <tr id="h_include" name="h_include">
	  		<th colspan="4" class="hapheader">Include hap?</th>
<?php 
			for ($h = 0; $h < $numhaps; $h++)
			{
				echo ("\t\t\t<td class=\"checkbox\"><input type=\"checkbox\" id=\"h".$h."is_include\" name=\"h".$h."is_include\" onchange=\"his_include_handler(this)\"></td>\n");
			}
?>
		</tr>
		<tr id="h_groups" name="h_groups">
			<th colspan="4" class="hapheader">Haplotype name:</th>
<?php
			for ($h = 0; $h < $numhaps; $h++)
			{
				echo ("\t\t\t<td class=\"stringname\"><input type=\"text\" id=\"hname".$h."\" name=\"hname".$h."\" onchange=\"hapname_handler(this)\"></td>\n");
			}
?>
			</tr>
			<th colspan="4" class="hapheader">Haplotype group:</th>
<?php
			for ($h = 0; $h < $numhaps; $h++)
			{
				echo ("\t\t\t<td class=\"stringname\"><select id=\"hgroup".$h."\" name=\"hgroup".$h."\" onchange=\"hapgroups_handler(this)\">\n");
				for ($g = 0; $g < count($uniquegroups); $g++)
				{
					echo ("\t\t\t\t<option value=\"".$uniquegroups[$g]."\">".$uniquegroups[$g]."</option>\n");
				}
				echo ("\t\t\t\t<option value=\"Add a new group\">Add a new group</option>\n");
				echo ("\t\t\t</select></td>\n");
			}
?>
		</tr>
	  	<tr id="theader" name="theader">
			<th>Include pop?</th>
			<th class="stringname">Pop name</th>
	  		<th class="latlng">Lat</th>
	  		<th class="latlng">Lng</th>
<?php
		for ($h = 0; $h < $numhaps; $h++)
			echo ("\t\t\t<th># hap".$h."</th>\n");
		echo ("\t\t</tr>\n");
		for ($p = 0; $p < $numpops; $p++)
		{
			echo ("\t\t<tr id=\"p".$p."\">\n");
			echo ("\t\t\t<td class=\"checkbox\"><input type=\"checkbox\" id=\"p".$p."is_include\" name=\"p".$p."is_include\" onchange=\"pis_include_handler(this)\"></td>\n");
			echo ("\t\t\t<td class=\"stringname\"><input type=\"text\" id=\"pname".$p."\" name=\"pname".$p."\" onchange=\"pop_names_handler(this)\"></td>\n");
			echo ("\t\t\t<td class=\"latlng\"><input type=\"text\" id=\"plat".$p."\" name=\"plat".$p."\" onchange=\"pop_lat_handler(this)\"></td>\n");
			echo ("\t\t\t<td class=\"latlng\"><input type=\"text\" id=\"plng".$p."\" name=\"plng".$p."\" onchange=\"pop_lng_handler(this)\"></td>\n");
			for ($h = 0; $h < $numhaps; $h++)
				echo ("\t\t\t<td class=\"number\"><input type=\"text\" id=\"p".$p."h".$h."\" name=\"p".$p."h".$h."\" onchange=\"pop_haps_handler(this)\"></td>\n");
			echo ("\t\t</tr>\n");
		}
?>
	</table>
  	</form>
	</div>
  	<p>
  	<table>
  		<tr><td></td><th>pops</th><th>haps</th></tr>
  		<tr>
  			<th>Total # included</th>
  			<td id="np" name="np" class="total"></td>
 			<td id="nh" name="nh" class="total"></td>
  		</tr>
	</table>
 	</p>
 	
	<form id="drawmap" name="drawmap" method="post" action="newviewer.php">
		<input type="hidden" id="numpops" name="numpops">
		<input type="hidden" id="numhaps" name="numhaps">
		<input type="hidden" id="pop_names" name="pop_names">
		<input type="hidden" id="hap_names" name="hap_names">		
		<input type="hidden" id="pop_lat" name="pop_lat">
		<input type="hidden" id="pop_lng" name="pop_lng">
		<input type="hidden" id="pop_haps" name="pop_haps">
		<input type="hidden" id="hapgroups" name="hapgroups">
		<input type="hidden" id="pop_include" name="pop_include">
		<input type="hidden" id="hap_include" name="hap_include">
		<input type="hidden" id="radii_in_km" name="radii_in_km">
		<input type="hidden" id="set_hap_colors" name="set_hap_colors">
	<p><input type="submit" value="Update Map!" onclick="serializeediteddata();"></p>
	</form>
  </div>

 </body>
</html>
